<?php
/**
 * Yotaku Shop - yshop
 * @author Creatint<creatint@163.com>
 * @copyright www.yotaku.cn 2016
 */
namespace Home\Model;
use Think\Model;
use Think\Upload;
class CartModel extends Model{
    public $error   =   '';
    //自动完成
    protected  $_validate = array(
        array('goods_id','require','商品ID必须!'),
        array('goods_title','require','商品标题必须!'),
    );
    //自动完成
    protected $_auto = array(
        array('create_time', NOW_TIME, self::MODEL_INSERT),
        array('update_time', NOW_TIME, self::MODEL_BOTH),
    );

    //购物车编辑时更新
    public function update($data){
        $map    =   array();
        $data['id']?($map['id']=$data['id']):false;
        $map['user_id'] =   $data['user_id']?$data['user_id']:0;
//        $map['goods_id'] = $data['goods_id']?$data['goods_id']:0;
//        $map['activity'] = $data['activity'];
        // 是否已存在该商品的购物车数据
        if($data['id'] && ($res = $this->where($map)->find())){
            $map['id'] = $res['id'];
            $this->where($map)->setInc('number',$data['number']);
            unset($data['number']);
            if($map['activity']>0){
                $data['status'] = 1;
            }
            $data['id'] = $res['id'];
            $this->where($map)->save($data);
        }else{
            if($this->create($data) && ($id = $this->add($data))){
                return $id;
            }else{
                return false;
            }
        }
        return true;
    }

    //提交订单前临时购物车
    public function confirm_update($data){
        //全部非活动商品临时购物车
        $map_noactivity    =   array();
        $map_noactivity['user_id'] =   $data['user_id']?$data['user_id']:0;
        $map_noactivity['activity'] = 0;
        $map_noactivity['status'] = 2;
        //全部当前商品ID临时购物车
        $map_current = array();
        $map_current['user_id'] =   $data['user_id']?$data['user_id']:0;
        $map_current['goods_id'] = $data['goods_id']?$data['goods_id']:0;
        $map_current['activity'] = $data['activity']?$data['activity']:0;
        $map_current['status'] = 1; ;

        //删除当前非活动商品ID的临时购物车数据
        $this->where($map_current)->delete();

        //清理非活动商品的临时购物车数据
        $this->where($map_noactivity)->delete();

        //添加临时购物车数据
        if(($data=$this->create($data)) && ($id=$this->add($data))){
            //添加成功
            return $id;
        }else{
            //添加失败
            return false;
        }

    }

    //通过UID获取购物车
    public function getCart($user_id){
        $map = array();
        $map['user_id'] = $user_id;
//        $map['status'] = 0;
        $res = $this->where($map)->select();
        return $res;
    }
    //通过ID获取购物车
    public function getCartsById($id){
        $map['id'] = is_array($id)?array("in",$id):$id;
        $res = $this->where($map)->select();
        return $res;
    }
    public function del($id,$user_id=0){
        if($user_id){
            $map = array('user_id'=>$user_id);
        }else{
            $this->error = '未登录';
            return false;
        }
        $id = explode(',',$id);
        if(empty($id)){
            $this->error = '未选择要删除的购物车';
        }elseif(is_array($id)){
            $map['id'] = array('IN',$id);
        }else{
            $map['id'] = $id;
        } 
        //$res = $this->where($map)->delete();
        if($this->where($map)->delete()){
            return true;
        }else{
            $this->error = '无此条数据';
            return false;
        }
    }
    public function getError(){
        return $this->error;
    }
}